import torch


def f1(pred, target):
    correct = torch.sum((pred == target)*target)
    precision = correct / (torch.sum(pred)+1e-9)
    recall = correct / (torch.sum(target)+1e-9)
    return 2 * (recall * precision) / (recall + precision + 1e-9)


def macro_f1(proba_pred, target, threshold):
    pred = proba_pred > threshold
    a = f1(pred,target)
    b = f1(~pred,1-target)
    return (a+b)/2


def compute_acc(proba_pred, target,threshold):
    pred = proba_pred > threshold
    correct = torch.sum(pred == target)
    return correct / (target.size()[0]*target.size()[1])


def auc(proba_pred, target):
    pass
